package cn.home.sysmanagement.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.home.common.annotation.Log;
import cn.home.common.core.controller.BaseController;
import cn.home.common.core.domain.AjaxResult;
import cn.home.common.enums.BusinessType;
import cn.home.sysmanagement.domain.HomeMedicine;
import cn.home.sysmanagement.service.IHomeMedicineService;
import cn.home.common.utils.poi.ExcelUtil;
import cn.home.common.core.page.TableDataInfo;

/**
 * 药品Controller
 * 
 * @author homedoctor
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/sysmanagement/medicine")
public class HomeMedicineController extends BaseController
{
    @Autowired
    private IHomeMedicineService homeMedicineService;

    /**
     * 查询药品列表
     */
    @PreAuthorize("@ss.hasPermi('sysmanagement:medicine:list')")
    @GetMapping("/list")
    public TableDataInfo list(HomeMedicine homeMedicine)
    {
        startPage();
        List<HomeMedicine> list = homeMedicineService.selectHomeMedicineList(homeMedicine);
        return getDataTable(list);
    }

    /**
     * 导出药品列表
     */
    @PreAuthorize("@ss.hasPermi('sysmanagement:medicine:export')")
    @Log(title = "药品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HomeMedicine homeMedicine)
    {
        List<HomeMedicine> list = homeMedicineService.selectHomeMedicineList(homeMedicine);
        ExcelUtil<HomeMedicine> util = new ExcelUtil<HomeMedicine>(HomeMedicine.class);
        util.exportExcel(response, list, "药品数据");
    }

    /**
     * 获取药品详细信息
     */
    @PreAuthorize("@ss.hasPermi('sysmanagement:medicine:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(homeMedicineService.selectHomeMedicineById(id));
    }

    /**
     * 新增药品
     */
    @PreAuthorize("@ss.hasPermi('sysmanagement:medicine:add')")
    @Log(title = "药品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HomeMedicine homeMedicine)
    {
        homeMedicine.setRelaseTime(new Date());
        return toAjax(homeMedicineService.insertHomeMedicine(homeMedicine));
    }

    /**
     * 修改药品
     */
    @PreAuthorize("@ss.hasPermi('sysmanagement:medicine:edit')")
    @Log(title = "药品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HomeMedicine homeMedicine)
    {
        return toAjax(homeMedicineService.updateHomeMedicine(homeMedicine));
    }

    /**
     * 删除药品
     */
    @PreAuthorize("@ss.hasPermi('sysmanagement:medicine:remove')")
    @Log(title = "药品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(homeMedicineService.deleteHomeMedicineByIds(ids));
    }
}
